//index.js
const app = getApp()

Page({
  data: {
    fileId: '',
    description: '',
    avatarUrl: './user-unlogin.png',
    userInfo: {},
    logged: false
  },

  onLoad: function() {
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              this.setData({
                avatarUrl: res.userInfo.avatarUrl,
                userInfo: res.userInfo
              })
            }
          })
        }
      }
    })
  },

  //授权方法
  onGetUserInfo: function(e) {
    if (!this.logged && e.detail.userInfo) {
      this.setData({
        logged: true,
        avatarUrl: e.detail.userInfo.avatarUrl,
        userInfo: e.detail.userInfo
      })
      this.onAdd();
    }
  },



  // 上传图片
  doUpload: function() {
    // 选择图片
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {

        wx.showLoading({
          title: '上传中',
        })

        const filePath = res.tempFilePaths[0]
        var mydate = new Date();
        var uuid = "cms" + mydate.getDay() + mydate.getHours() + mydate.getMinutes() + mydate.getSeconds() + mydate.getMilliseconds();
        // 上传图片
        const cloudPath = 'bug_img/' + uuid + filePath.match(/\.[^.]+?$/)[0]
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: res => {
            console.log('[上传文件] 成功：', res)

            that.setData({
              fileId: res.fileID
            })
          },
          fail: e => {
            console.error('[上传文件] 失败：', e)
            wx.showToast({
              icon: 'none',
              title: '上传失败',
            })
          },
          complete: () => {
            wx.hideLoading()
          }
        })

      },
      fail: e => {
        console.error(e)
      }
    })
  },
  //表单提交
  onAdd: function() {
    const db = wx.cloud.database()
    var description = this.data.description;
    var fileId = this.data.fileId;
    var nickname = this.data.userInfo.nickName;
    var userimg = this.data.avatarUrl;
    var createTime = new Date();
    if (description == '' && fileId == '') {
      wx.showToast({
        title: '空数据',
      })
      return;
    }
    console.log(123);
    db.collection('bug_record').add({
      data: {
        description: description,
        fileId: fileId,
        nickname: nickname,
        userimg: userimg,
        createTime: createTime
      },
      success: res => {
        // 在返回结果中会包含新创建的记录的 _id

        wx.showToast({
          title: '新增记录成功',
        })
        console.log('[数据库] [新增记录] 成功，记录 _id: ', res._id)
      },
      fail: err => {
        wx.showToast({
          icon: 'none',
          title: '新增记录失败'
        })
        console.error('[数据库] [新增记录] 失败：', err)
      }
    })
  },
  //记录bug描述内容
  bugDesInput: function(e) {
    this.setData({
      description: e.detail.value
    })
  },
  myBug:function(){

    wx.navigateTo({
      url: '../list/index',
    })
  }
})